package com.klangner.go.app.rules;

// implementation class for a "list of liberties";
//for porting sake this is general;
//can be implemented as Vector, Linked list, or whatever
// it's important not to have duplicates in this list!

import java.util.*;


public class ListOfLiberties {
   private Vector hiddenList;

   //the default constructor
   public ListOfLiberties() {
      hiddenList = new Vector();
   }

   public ListOfLiberties(Vector hiddenList) {
     this.hiddenList = hiddenList;
   }

   public boolean isNone(){
     return (hiddenList.size() == 0); 
   }

   //check if a liberty is already in the list
   public boolean contain(StonesGroupOld libertyToSearch) {
      return hiddenList.contains(libertyToSearch);
   }

   // the copy-constructor
   public ListOfLiberties(ListOfLiberties copyFrom) {
      this.hiddenList = new Vector();
      hiddenList = copyFrom.hiddenList;
   }


   // add an element to the  end of the list
   public void add (GobanLocation toAdd) {
     if (!hiddenList.contains(toAdd))
       hiddenList.addElement(toAdd);
   }

   // get the number of elements in the list
   public int length() {
      return hiddenList.size();
   }

   // get the element in the list at the given position
   public GobanLocation getElementAt(int index) {
      return (GobanLocation)hiddenList.elementAt(index);
   }

   //get the first element
   public GobanLocation first() {
      return (GobanLocation)hiddenList.firstElement();
   }

   // delete the element in the list at the given position
   public void deleteElementAt(int index) {
      hiddenList.removeElementAt(index);
   }

   // delete the given element in the list
   public void deleteElement(GobanLocation libertyToDelete) {
     hiddenList.removeElement(libertyToDelete);
   }

   // get all the elements in the list
   public Enumeration getElements() {
      return hiddenList.elements();
   }

   // append another list to the end 
   public void appendList(ListOfLiberties listToAppend) {
      for (Enumeration e = listToAppend.getElements(); e.hasMoreElements();) {

         GobanLocation currentLiberty = (GobanLocation)e.nextElement();

         this.add(currentLiberty);
      }
   } // appendList

   public void removeAllElements() {
     hiddenList.removeAllElements();
   }

   public synchronized Object clone() {
     return new ListOfLiberties((Vector)hiddenList.clone());
   }

   public String toString() {
     return hiddenList.toString();
   }
}
